/*
 Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
 Original sources are available at www.latestbit.com

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

 RequestContext.cpp. Created on: 08.09.2009 12:09:05
 */

#include "smartobjects/RequestContext.hpp"
#include "smartobjects/core/ServerSkeleton.hpp"
#include "smartobjects/network/ConnectionHandler.hpp"

namespace smartobjects {

const unsigned int RequestContext::DEFAULT_COMMAND_OPERATION_TIMEOUT = 30; // 30 seconds
const std::string RequestContext::RC_OBJECT_TYPE = "org.latestbit.smartobjects.RequestContext";

RequestContext::RequestContext() :
		m_nResponseTimeout(DEFAULT_COMMAND_OPERATION_TIMEOUT),
		m_sLocale(OSTools::getCurrentLocale()),
		m_poSmartBus(NULL),
		m_poSkeleton(NULL),
		m_poConnectionHandler(NULL) {
}

RequestContext::~RequestContext() {
	releaseAndClear(m_poSkeleton);
	releaseAndClear(m_poConnectionHandler);
	releaseAndClear(m_poSmartBus);
}

void RequestContext::serialize(IDataTypesCoder* poCoder, IDataEncrypter* poEncrypter, ByteBuffer* poToBuffer) const {
	poCoder->encodeInt32(m_nResponseTimeout, poToBuffer);
	poCoder->encodeString(m_sLocale, poToBuffer);
	poCoder->encodeString(m_sTimezone, poToBuffer);
	if(m_aUserContextBuffer.empty())
		poCoder->encodeNull(poToBuffer);
	else
		poCoder->encodeUByteArray(m_aUserContextBuffer, poToBuffer);
}

void RequestContext::deserialize(IDataTypesCoder* poCoder, IDataEncrypter* poEncrypter, ByteBuffer* poFromBuffer) {
	m_nResponseTimeout = poCoder->decodeInt32(poFromBuffer);
	poCoder->decodeString(m_sLocale, poFromBuffer);
	poCoder->decodeString(m_sTimezone, poFromBuffer);
	poCoder->decodeUByteArray(m_aUserContextBuffer, poFromBuffer);
}

const std::string& RequestContext::getObjectType() const {
	return RC_OBJECT_TYPE;
}

ISmartBus* RequestContext::getBus() {
	return m_poSmartBus;
}

void RequestContext::setBus(ISmartBus* poBus) {
	setNewRefObject(m_poSmartBus, poBus);
}

ServerSkeleton* RequestContext::getSkeleton() {
	return m_poSkeleton;
}

void RequestContext::setSkeleton(class ServerSkeleton* poSkeleton) {
	setNewRefObject(m_poSkeleton, poSkeleton);
}

ConnectionHandler* RequestContext::getConnectionHandler() {
	return m_poConnectionHandler;
}

void RequestContext::setConnectionHandler(class ConnectionHandler* poConnectionHandler) {
	setNewRefObject(m_poConnectionHandler, poConnectionHandler);
}

void RequestContext::setUserContextBuffer(const std::vector<unsigned char>& aBuffer) {
	m_aUserContextBuffer = aBuffer;
}

std::vector<unsigned char>& RequestContext::getUserContextBuffer() {
	return m_aUserContextBuffer;
}

unsigned int RequestContext::getMaxResponseTimeoutInSec() {
	return m_nResponseTimeout;
}

void RequestContext::setMaxResponseTimeoutInSec(unsigned int nValue) {
	m_nResponseTimeout = nValue;
}

}
